package com.totofgeek.contactsboard.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.totofgeek.contactsboard.bean.Bean;
import com.totofgeek.contactsboard.bean.Contact;
import com.totofgeek.contactsboard.outils.utilitaires.StringUtilitaire;

/**
 * Classe abstraite pour la gestion des Adapteurs
 * 
 * @author Totof
 * 
 */
public abstract class AbstractAdapter extends BaseAdapter
{
	/*----------------------------------------------
	 * CONSTANTES
	 */

	/*----------------------------------------------
	 * VARIABLES
	 */

	/**
	 * Liste des objets
	 */
	private ArrayList<Bean>	listeBeansComplete;
	private ArrayList<Bean>	listeBeansFiltree;

	protected Context		context;

	/**
	 * Inflater
	 */
	private LayoutInflater	inflater;

	/**
	 * Constructeur de l'adapteur de la liste des contacts
	 * 
	 * @param context
	 * @param listeBeans
	 * @param isColored
	 */
	@SuppressWarnings("unchecked")
	public AbstractAdapter(Context context, Object[] listeBeans)
	{
		ArrayList<Bean> temp = new ArrayList<Bean>();
		for (Object bean : listeBeans)
			temp.add((Bean) bean);

		this.listeBeansFiltree = (ArrayList<Bean>) temp.clone();
		this.listeBeansComplete = (ArrayList<Bean>) temp.clone();
		this.inflater = LayoutInflater.from(context);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount()
	{
		return listeBeansFiltree.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	public Bean getItem(int position)
	{
		return listeBeansFiltree.get(position);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	public long getItemId(int position)
	{
		return position;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	public abstract View getView(int position, View vue, ViewGroup groupeVue);

	/**
	 * @param bean
	 */
	public void retirerBean(Bean bean)
	{
		if (listeBeansComplete.contains(bean))
			listeBeansComplete.remove(bean);

		if (listeBeansFiltree.contains(bean))
			listeBeansFiltree.remove(bean);

		this.notifyDataSetChanged();
	}

	/**
	 * @return
	 */
	public ArrayList<Bean> getListeBeansComplete()
	{
		return listeBeansComplete;
	}

	/**
	 * @return
	 */
	public ArrayList<Bean> getListeBeansFiltree()
	{
		return listeBeansFiltree;
	}

	/**
	 * @return the inflater
	 */
	public LayoutInflater getInflater()
	{
		return inflater;
	}

	/**
	 * @param position
	 */
	public void removeViewAt(int position)
	{
		listeBeansFiltree.remove(position);

		this.notifyDataSetChanged();
	}

	/**
	 * @param bean
	 */
	public void add(Bean bean)
	{
		if (!listeBeansComplete.contains(bean))
			listeBeansComplete.add(bean);

		if (!listeBeansFiltree.contains(bean))
			listeBeansFiltree.add(bean);

		this.notifyDataSetChanged();
	}

	/**
	 * @param bean
	 */
	public void modifierElement(Bean bean)
	{
		modifierElement(bean, Boolean.TRUE);
	}

	/**
	 * @param bean
	 */
	public void modifierElement(Bean bean, Boolean notifyDataSetChanged)
	{
		int index = listeBeansComplete.indexOf(bean);
		if (index != -1)
			listeBeansComplete.set(index, bean);
		else
			listeBeansComplete.add(bean);

		index = listeBeansFiltree.indexOf(bean);
		if (index != -1)
			listeBeansFiltree.set(index, bean);
		else
			listeBeansFiltree.add(bean);

		if (notifyDataSetChanged)
			this.notifyDataSetChanged();
	}

	/**
	 * @return
	 */
	public int size()
	{
		return listeBeansFiltree.size();
	}

	/**
	 * @param contact
	 * @return
	 */
	public Boolean contient(Bean contact)
	{
		return listeBeansFiltree.contains(contact);
	}

	/**
	 * @param position
	 * @param contact
	 */
	public void changerBean(int position, Bean contact)
	{
		listeBeansFiltree.set(position, contact);
	}

	/**
	 * @param listeBeans
	 */
	@SuppressWarnings("unchecked")
	public void setListeBeansComplete(Object[] listeBeans)
	{
		ArrayList<Bean> temp = new ArrayList<Bean>();
		for (Object bean : listeBeans)
			temp.add((Bean) bean);

		if (this.listeBeansComplete == null)
			this.listeBeansComplete = new ArrayList<Bean>();
		else
			this.listeBeansComplete.clear();

		this.listeBeansComplete = (ArrayList<Bean>) temp.clone();

		this.notifyDataSetChanged();
	}

	/**
	 * @param listeBeans
	 */
	@SuppressWarnings("unchecked")
	public void setListeBeansFiltree(Object[] listeBeans)
	{
		ArrayList<Bean> temp = new ArrayList<Bean>();
		for (Object bean : listeBeans)
			temp.add((Bean) bean);

		if (this.listeBeansFiltree == null)
			this.listeBeansFiltree = new ArrayList<Bean>();
		else
			this.listeBeansFiltree.clear();

		this.listeBeansFiltree = (ArrayList<Bean>) temp.clone();

		this.notifyDataSetChanged();
	}

	/**
	 * @param filtre
	 */
	@SuppressLint("DefaultLocale")
	public void filtrerListe(String filtre)
	{
		if (filtre == null)
			filtre = "";

		ArrayList<Contact> listTemp = new ArrayList<Contact>();

		String nom = null;
		for (Bean bean : getListeBeansComplete())
		{
			nom = StringUtilitaire.sansAccent(((Contact) bean).getNom().toLowerCase());
			if (nom != null && nom.contains(filtre.toLowerCase()))
				listTemp.add((Contact) bean);
		}

		setListeBeansFiltree(listTemp.toArray());
	}
}
